package com.ymatou.iapi.productmanager.testcase;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.GetSetTopProductsCall;
import com.ymatou.iapi.productmanager.service.SetTopProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.utils.EnvSetup;


public class Ts_GetSetTopProducts {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取橱窗列表");
		//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(sellerId);
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetSetTopProductsCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	//获取橱窗列表
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSetTopProducts() {
		Logger.start(true, "获取橱窗列表");
		try{
			//发布商品
			//String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductId");
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject().get("ProductId").getAsString();

			//添加到橱窗
			Map<String, Object> updateTopMap = new HashMap<String,Object>();
			updateTopMap.put("UserId", sellerId);
			updateTopMap.put("ProductIds", new JSONArray("["+productId+"]"));
			updateTopMap.put("IsSetTop", true);
			
			new SetTopProductCall().callAndGetReturnData(updateTopMap);
			
			Map<String, Object> updateGetTopMap = new HashMap<String,Object>();
			updateGetTopMap.put("UserId", sellerId);
			JSONObject retObj = new GetSetTopProductsCall().callAndGetReturnData(updateGetTopMap);
			
			Logger.verifyEquals(200,retObj.getInt("Code"), "响应报文-返回码");
			Logger.verifyEquals(true,retObj.getJSONObject("Data").getJSONArray("Products").length()>0,"响应报文-Data");
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	

}
